package com.blockmeta.bbs.businesslibrary.net.okhttp;

import android.content.Context;
import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.net.callback.MyStringCallback;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.callback.ApiStringCallBack;
import com.blockmeta.bbs.businesslibrary.net.pojo.ErrorPOJO;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by author on 2017/12/28.
 */

public class OKHttpPostReq {
    public Context context;
    public String url;
    public HashMap<String, String> map;

    public OKHttpPostReq(Context context, String url, HashMap<String, String> map) {
        this.context = context;
        this.url = url;
        this.map = map;
    }


    /**
     * 必须塞入Token 和From
     */
    public void postData(final Callback callback) {
        OkHttpUtils.post()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .params(map)
                .url(url)
                .build()
                .execute(new ApiStringCallBack(context) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onResponse(response, id);
                    }
                });
    }

    public void postDataComment(final Callback callback) {
        if (!AccountHelper.isLogin()) {
            RouterHelper.startActivity(RouterPath.PHONE_LOGIN_ACTIVITY);
            return;
        }
        ProgressBarUtil.showProgress(context);
        OkHttpUtils.postString()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .addHeader("uid", AccountHelper.getUid())
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new Gson().toJson(map))
                .url(url)
                .build()
                .execute(new ApiStringCallBack(context) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        ProgressBarUtil.dismissProgress(context);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        if (code == 200) {
                            callback.onResponse(response, id);
                        } else {
                            ErrorPOJO errorPOJO = GsonUtil.buildGson().fromJson(response, ErrorPOJO.class);
                            ToastUtil.show(errorPOJO.getMessage());
                        }

                    }
                });
    }

    public void postDataWithJson(final Callback callback) {
        ProgressBarUtil.showProgress(context);
        OkHttpUtils.postString()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new Gson().toJson(map))
                .url(url)
                .build()
                .execute(new ApiStringCallBack(context) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        ProgressBarUtil.dismissProgress(context);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ProgressBarUtil.dismissProgress(context);
                        if (code == 200) {
                            callback.onResponse(response, id);
                        } else if (code == 300) {
                            ErrorPOJO errorPOJO = GsonUtil.buildGson().fromJson(response, ErrorPOJO.class);
                            ToastUtil.show(errorPOJO.getMessage());
                        } else {

                        }
                    }
                });
    }

    /**
     * 点赞功能
     * @param callback
     * @param pid
     */
    public void postUid(final Callback callback,String pid){
        OkHttpUtils.post()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(context, Constant.TOKEN, ""))
                .addHeader("uid",AccountHelper.getUid())
                .params(map)
                .url(url)
                .build()
                .execute(new MyStringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callback.onResponse(response, id);
                    }
                });
    }


}
